#!usr/bin/python3
import sys
import getopt
import os
import shutil


def getDirFromDir(path):
    """
    获取该目录下的所有子目录
    :param path:
    :return:
    """
    L = []
    for dirpath, dirnames, filenames in os.walk(path):
        return dirnames


def getFilePathFromDir(path, suffixes):
    """
    获取该目录下的所有文件完整路径，不递归
    :param path:
    :return:
    """
    L = []
    for dirpath, dirnames, filenames in os.walk(path):
        for item in filenames:
            if item[0] == ".":
                continue
            if os.path.splitext(item)[1] in suffixes:
                L.append(os.path.join(dirpath, item))
        return L


def main(argv):
    dec = """用于划分文件，将文件下的所有图片分开存储到指定文件夹下
        可用参数 
        -s | size 分类文件大小
    """
    originPath = argv[0]  # 原始路径
    # targetPath = argv[1]  # 目标路径
    print("originPath", originPath)
    # print("targetPath", targetPath)
    classDirList = getDirFromDir(originPath)
    print("find class dir is {0}".format(classDirList))
    for item in classDirList:
        currentClassPath = originPath + "/" + item  # 当前类路径
        notClassFile = getFilePathFromDir(currentClassPath, [".png", ".jpg", ".jpeg", ".xml"])
        for element in notClassFile:  # 删除类路径下未分类的文件
            print("remove {0} ".format(element))
            os.remove(element)
        testPath = currentClassPath + "/test"
        trainPath = currentClassPath + "/train"
        dataList = getFilePathFromDir(testPath, [".png", ".jpg", ".jpeg", ".xml"]) + getFilePathFromDir(trainPath, [".png", ".jpg", ".jpeg", ".xml"])
        for element in dataList:
            print("move {0}  to {1}".format(element, currentClassPath))
            shutil.move(element, currentClassPath)
        shutil.rmtree(testPath)
        shutil.rmtree(trainPath)


if __name__ == "__main__":
    main(sys.argv[1:])
